<?php
// No directAcces
defined( '_AppBackMiage' ) or die( 'Restricted access' );
if(isAdministrateur ()){
    include_once (DS."backoffice/include/header.php");


            if(isset($_POST['submit']))
            {
                $etudiant = new Etudiant();
                // variable qui va nous servir pour savoir s'il y a a eu une erreur ;)
                $form = true;
                $errMSG = array();
                if(isset($_POST['lastname']) && !empty($_POST['lastname']))
                {
                    $tmp = $etudiant->setNomUtilisateur($_POST['lastname']);
                    if($tmp['boolOk'] === false)
                    {
                        $errMSG['nom'] = $tmp['errmsg'];
                        $form = false;
                    }
                }
                if(isset($_POST['firstname']) && !empty($_POST['firstname']))
                {
                    $tmp = $etudiant->setPrenomUtilisateur($_POST['firstname']);
                    if($tmp['boolOk'] === false)
                    {
                        $errMSG['prenom'] = $tmp['errmsg'];
                        $form = false;
                    }
                }
                if(isset($_POST['password']) && !empty($_POST['password']) && isset($_POST['password2']) && !empty($_POST['password2']))
                {
                    $tmp = $etudiant->setPassword($_POST['password']);
                    if($tmp['boolOk'] === false)
                    {
                        $errMSG['password'] = $tmp['errmsg'];
                        $form = false;
                    }
                }
                if(isset($_POST['email']) && !empty($_POST['email']))
                {
                    $tmp = $etudiant->setEmailUtilisateur($_POST['email']);
                    if($tmp['boolOk'] === false)
                    {
                        $errMSG['mail'] = $tmp['errmsg'];
                        $form = false;
                    }
                }

                if(isset($_POST['adresse']) && !empty($_POST['adresse']))
                {
                    $tmp = $etudiant->setAdresse1($_POST['adresse']);
                    if($tmp['boolOk'] === false)
                    {
                        $errMSG['add1'] = $tmp['errmsg'];
                        $form = false;
                    }
                }
                if(isset($_POST['adresse2']) && !empty($_POST['adresse2']))
                {
                    $tmp = $etudiant->setAdresse2($_POST['adresse2']);
                    if($tmp['boolOk'] === false)
                    {
                        $errMSG['add2'] = $tmp['errmsg'];
                        $form = false;
                    }
                }
                if(isset($_POST['city']) && !empty($_POST['city']))
                {
                    $tmp = $etudiant->setVille($_POST['city']);
                    if($tmp['boolOk'] === false)
                    {
                        $errMSG['ville'] = $tmp['errmsg'];
                        $form = false;
                    }
                }
                if(isset($_POST['cp']) && !empty($_POST['cp']))
                {
                    $tmp = $etudiant->setCP($_POST['cp']);
                    if($tmp['boolOk'] === false)
                    {
                        $errMSG['cp'] = $tmp['errmsg'];
                        $form = false;
                    }
                }
                if(isset($_POST['promo']) && !empty($_POST['promo']))
                {
                    $tmp = $etudiant->setIdPromo($_POST['promo']);
                    if($tmp['boolOk'] === false)
                    {
                        $errMSG['promo'] = $tmp['errmsg'];
                        $form = false;
                    }
                }

                if(isset($_POST['types']) && !empty($_POST['types']))
                {
                    $types = implode(',',$_POST['types']);
                    $tmp = $etudiant->setIdTypeUser($types);
                    if($tmp['boolOk'] === false)
                    {
                        $errMSG['types'] = $tmp['errmsg'];
                        $form = false;
                    }
                }

                if($form === true)
                {
                    $etudiant->insert();
                    redirectTo(URLBACK."_useretudiant_list");
                }
            }
    $lstPromo = Promo::getListPromo();
    $lstTypes = TypeUtilisateur::getList();
    ?>
    <?php

    include_once( DS."backoffice/include/toppanel_utilisateurs.php");

    ?>
    <div id="wrapper">
        <div id="content">
            <div id="box">
                <h3 id="adduser">Ajout d'un &eacute;tudiant</h3>
                <form id="form" method="post">
                <fieldset id="personal">
                    <legend>Informations Personnelles</legend>
                    <label for="lastname">Nom : </label>
                    <input name="lastname" id="lastname" type="text" tabindex="1" />
                    <br />
                    <label for="firstname">Prenom : </label>
                    <input name="firstname" id="firstname" type="text" tabindex="2" />
                    <br />
                    <label for="password">Password : </label>
                    <input name="password" id="password" type="text"tabindex="2" />
                    <br />
                    <label for="password2">re-Password : </label>
                    <input name="password2" id="password2" type="text"tabindex="2" />
                    <br />
                    <label for="email">Email : </label>
                    <input name="email" id="email" type="text"tabindex="2" />
                </fieldset>
                <fieldset id="address">
                    <legend>Adresse</legend>
                    <label for="adresse">Adresse : </label>
                    <input name="adresse" id="adresse" type="text" tabindex="1" />
                    <br />
                    <label for="adresse2">Adresse + : </label>
                    <input name="adresse2" id="adresse2" type="text" tabindex="1" />
                    <br />
                    <label for="city">Ville : </label>
                    <input name="city" id="city" type="text" tabindex="2" />
                    <br />
                    <label for="cp">Code Postal : </label>
                    <input name="cp" id="cp" type="text" tabindex="1" />
                    <br />
                </fieldset>
                <fieldset id="opt">
                    <legend>OPTIONS</legend>
                    <label for="promo">Promo : </label>
                    <select name="promo">
                    <?php
                    if(!empty($lstPromo))
                    {
                        foreach($lstPromo as $id=>$p)
                        {
                            ?>
                            <option value="<?php echo $id; ?>"><?php echo $p; ?></option>
                            <?php
                        }
                    }
                    ?>
                    </select>
                    <br />
                    <label for="types">Type d'utilisateur : </label>
                    <?php
                    if(!empty($lstTypes))
                    {
                        foreach($lstTypes as $type)
                        {
                            ?>
                            <?php echo $type->NomTypeUtilisateur; ?>
                            <input type="checkbox" name="types[]" value="<?php echo $type->IdTypeUtilisateur; ?>">
                            &nbsp;|&nbsp;
                            <?php
                        }
                    }
                    ?>
                    <br /><span class="error"><?php if(isset($errMSG['types'])){ echo $errMSG['types']; } ?></span>
                </fieldset>
                <div align="center">
                    <input id="button1" type="submit" name="submit" value="Ajouter" />
                    <input id="button2" type="reset" />
                </div>
                </form>
            </div>
            </div>
    <?php
    include_once (DS."backoffice/include/sidebar.php");
    include_once (DS."backoffice/include/footer.php");
 }
else
{
    redirectTo(URLBACK);
}
?>